package com.aspose.words.maven.wizard;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.aspose.words.maven.wizard.ResourceFactory.AsposeWizardResources;

/**
 * @author Affan Hasan
 *
 */
public class AsposeWordMavenProjectWizardPage extends WizardPage {

	private Text projectName;
	private Text projectLocation;
	private Text projectFolder;

	private Text artifactId;
	private Text groupId;
	private Text version;
	private Text packageName;

	private final String workSpaceRoot = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().toString();

	/**
	 * Create the wizard.
	 */
	public AsposeWordMavenProjectWizardPage() {
		super("wizardPage");
		setTitle("New Aspose Words Maven Project");
		setDescription("Project Name And Location");
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {

		Composite container = new Composite(parent, SWT.NULL);
		// Grid Layout
		GridLayout layout = new GridLayout();
		// Layout For Container
		container.setLayout(layout);
		layout.numColumns = 3;
		layout.verticalSpacing = 9;
		GridData gd;

		try (InputStream is = ResourceFactory.getResourceAsInputStream(AsposeWizardResources.BANNER_IMG)) {
			Image banner = new Image(container.getDisplay(), is);
			Label lbl = new Label(container, SWT.NONE);
			gd = new GridData(GridData.FILL_HORIZONTAL & SWT.BOLD);
			gd.horizontalSpan = 3;
			lbl.setLayoutData(gd);
			lbl.setImage(banner);
		} catch (IOException e) {
			e.printStackTrace();
		}

//		 Project Name Label
		Label projectDetailLabel = new Label(container, SWT.BOLD);
		projectDetailLabel.setText("Please enter project detail: ");
		gd = new GridData(GridData.FILL_HORIZONTAL & SWT.BOLD);
		gd.horizontalSpan = 3;
		projectDetailLabel.setLayoutData(gd);

//		 Project Name Label
		Label label = new Label(container, SWT.NULL);
		label.setText("&Project Name: ");

//		 Project Name Text
		this.projectName = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		this.projectName.setLayoutData(gd);
		this.projectName.setText(calculateProjectName());
		this.projectName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				artifactId.setText(projectName.getText());
				projectFolder.setText(getProjectFolderValue());
			}
		});

//		 Project Location Label
		Label projectLocationLabel = new Label(container, SWT.NULL);
		gd = new GridData(GridData.BEGINNING);
		projectLocationLabel.setLayoutData(gd);
		projectLocationLabel.setText("Project Location: ");

//		 Project Location Value
		this.projectLocation = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.BEGINNING);
		this.projectLocation.setLayoutData(gd);
		this.projectLocation.setText(this.workSpaceRoot);

//		 Browse Button
		Button button = new Button(container, SWT.PUSH);
		button.setText("Browse");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				handleBrowse();
			}
		});

//		 Project Folder Label
		Label projectFolderLabel = new Label(container, SWT.NULL);
		projectFolderLabel.setText("Project Folder: ");

//		 Project Folder Value
		this.projectFolder = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		this.projectFolder.setLayoutData(gd);
		this.projectFolder.setText(getProjectFolderValue());
		this.projectFolder.setEnabled(false);

//		 Maven Artifact Label
		Label mavenArtifactLabel = new Label(container, SWT.BOLD);
		mavenArtifactLabel.setText("Please enter maven artifact detail: ");
		gd = new GridData(GridData.FILL_HORIZONTAL & SWT.BOLD);
		gd.horizontalSpan = 3;
		mavenArtifactLabel.setLayoutData(gd);

//		 Artifact Id Label
		final Label artifactIdLabel = new Label(container, SWT.NULL);
		artifactIdLabel.setText("Artifact Id: ");

//		 Artifact Value Text
		this.artifactId = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		this.artifactId.setLayoutData(gd);
		this.artifactId.setText(this.projectName.getText());
		this.artifactId.setEnabled(false);

//		 Group Id Label
		final Label groupIdLabel = new Label(container, SWT.NULL);
		groupIdLabel.setText("Group Id: ");

//		 Group Value Text
		this.groupId = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		this.groupId.setLayoutData(gd);
		this.groupId.setText("com.mycompany");

//		 Version Label
		final Label versionLabel = new Label(container, SWT.NULL);
		versionLabel.setText("Version: ");

//		 Version Value Text
		this.version = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		this.version.setLayoutData(gd);
		this.version.setText("1.0-SNAPSHOT");

//		 Package Label
		final Label packageLabel = new Label(container, SWT.NULL);
		packageLabel.setText("Package: (Optional) ");

//		 Package Value Text
		this.packageName = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		this.packageName.setLayoutData(gd);
		this.packageName.setText("com.mycompany." + this.getProjectName().getText());

		setControl(container);
	}
	
	private void handleBrowse() {

		DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.OPEN);
		dialog.setFilterPath(this.workSpaceRoot);
		String result = dialog.open();
		this.projectLocation.setText(result);
		this.projectFolder.setText(getProjectFolderValue());
	}
	
	private String calculateProjectName() {
		Set<String> projectNames = new LinkedHashSet<>();
		for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects(IResource.HIDDEN)) {
			projectNames.add(project.getName());
		}
		int counter = 1;
		do {
			if (!projectNames.contains("asposemavenproject" + counter)) {
				return "asposemavenproject" + counter;
			} else {
				counter++;
				continue;
			}
		} while (true);
	}
	
	private String getProjectFolderValue() {
		return this.projectLocation.getText() + "/" + this.projectName.getText();
	}

//	Getters
	
	public Text getProjectName() {
		return projectName;
	}

	public Text getProjectLocation() {
		return projectLocation;
	}

	public Text getProjectFolder() {
		return projectFolder;
	}

	public Text getArtifactId() {
		return artifactId;
	}

	public Text getGroupId() {
		return groupId;
	}

	public Text getVersion() {
		return version;
	}

	public Text getPackageName() {
		return packageName;
	}
}